from mcp.server.fastmcp import FastMCP, Context
import subprocess
import json
from typing import List

mcp = FastMCP("Libsndfile MCP Server")

"""
面向 libsndfile 软件的 MCP Server

MCP tools 列表：
1. sndfile_info - 获取音频文件信息
   参数：input_file (str) - 输入音频文件路径
   返回：包含文件信息的统一JSON结构

2. sndfile_convert - 转换音频文件格式
   参数：input_file (str) - 输入音频文件路径
         output_file (str) - 输出音频文件路径
   返回：转换结果的统一JSON结构

3. sndfile_validate - 验证音频文件是否存在
   参数：file_path (str) - 要验证的音频文件路径
   返回：验证结果的统一JSON结构
"""

@mcp.tool()
def sndfile_info(input_file: str) -> dict:
    """
    使用 sndfile-info 命令获取音频文件信息
    
    参数:
        input_file: 输入音频文件路径
        
    返回:
        JSON结构: {
            "success": bool,
            "command": str,
            "exit_code": int,
            "stdout": str,
            "stderr": str
        }
    """
    command = f"sndfile-info {input_file}"
    try:
        result = subprocess.run(command, shell=True, capture_output=True, text=True, timeout=30)
        return {
            "success": result.returncode == 0,
            "command": command,
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except Exception as e:
        return {
            "success": False,
            "command": command,
            "exit_code": -1,
            "stdout": "",
            "stderr": str(e)
        }

@mcp.tool()
def sndfile_convert(input_file: str, output_file: str) -> dict:
    """
    使用 sndfile-convert 命令转换音频文件格式
    
    参数:
        input_file: 输入音频文件路径
        output_file: 输出音频文件路径
        
    返回:
        JSON结构: {
            "success": bool,
            "command": str,
            "exit_code": int,
            "stdout": str,
            "stderr": str
        }
    """
    command = f"sndfile-convert {input_file} {output_file}"
    try:
        result = subprocess.run(command, shell=True, capture_output=True, text=True, timeout=60)
        return {
            "success": result.returncode == 0,
            "command": command,
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except Exception as e:
        return {
            "success": False,
            "command": command,
            "exit_code": -1,
            "stdout": "",
            "stderr": str(e)
        }

@mcp.tool()
def sndfile_validate(file_path: str) -> dict:
    """
    验证音频文件是否存在
    
    参数:
        file_path: 要验证的音频文件路径
        
    返回:
        JSON结构: {
            "success": bool,
            "command": str,
            "exit_code": int,
            "stdout": str,
            "stderr": str
        }
    """
    command = f"test -e {file_path}"
    try:
        result = subprocess.run(command, shell=True, capture_output=True, text=True, timeout=10)
        return {
            "success": result.returncode == 0,
            "command": command,
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except Exception as e:
        return {
            "success": False,
            "command": command,
            "exit_code": -1,
            "stdout": "",
            "stderr": str(e)
        }

if __name__ == "__main__":
    mcp.run()